package com.mcptt.config.body.location;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.cloudptt.api.comm.CheckData;

public class LocationInfoBody_Configuration {
	private static final String End_Mcptt_Tag = "</location-info>";

//	private static final String Name_Configuration = "Configuration";
//	private static final String Name_ConfigScope = "ConfigScope";
	private static final String Name_PeriodicReport = "PeriodicReport";
	private static final String Name_MinimumInterval = "minimumIntervalLength";
//	private static final String Name_TravelledDistance = "TravelledDistance";
	private static final String Name_TriggerId = "TriggerId";

//	private String scop = "Update";
	private String triggerId = null;
	private int timeInterval = 300;
	private int minimumInterval = 100;

	public LocationInfoBody_Configuration()
	{
//		this.scop = "";
		this.triggerId = "";
	}
	public LocationInfoBody_Configuration(String mcpttId,int timeInterval,int distanceInterval)
	{
		this.triggerId = mcpttId;
		this.timeInterval = timeInterval;
		this.minimumInterval = distanceInterval;
	}


	public static LocationInfoBody_Configuration parseXmlStr(String xmlStr) throws Exception
	{
		LocationInfoBody_Configuration body = new LocationInfoBody_Configuration();

		int index = xmlStr.indexOf(End_Mcptt_Tag);
//	    Log.d(THIS_FILE, "McpttInfoBody_NewCall parseXmlStr index = " + index);

//	    if(index <= 0)
//	    {
//	    	key = End_Mcvideo_Tag;
//	    	index = xmlStr.indexOf(key);
//	    }
		xmlStr= xmlStr.substring(0, index + End_Mcptt_Tag.length());
		xmlStr = xmlStr.trim();

		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据        
		int event = pullParser.getEventType();

		while (event != XmlPullParser.END_DOCUMENT){

			switch (event) {

				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if (Name_PeriodicReport.equals(pullParser.getName()))
					{
						body.triggerId = pullParser.getAttributeValue("", Name_TriggerId);
						String value = pullParser.nextText();
						body.timeInterval = CheckData.parseInt(value);
					}
					else if (Name_MinimumInterval.equals(pullParser.getName()))
					{
//						body.mcpttId = pullParser.getAttributeValue("", Name_TriggerId);
						String value = pullParser.nextText();
						body.minimumInterval = CheckData.parseInt(value);
					}
					break;

				case XmlPullParser.END_TAG:
					break;

			}

			event = pullParser.next();
		}

		return body;
	}

//	public String getScop()
//	{
//		return scop;
//	}
	public String getTriggerId()
	{
		return triggerId;
	}
	public int getTimeInterval()
	{
		return timeInterval;
	}
	public int getMinimumInterval()
	{
		return minimumInterval;
	}

	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
//		strBuff.append("scop = ").append(scop).append(",");
		strBuff.append("mcpttId = ").append(triggerId).append(",");
		strBuff.append("timeInterval = ").append(timeInterval).append(",");
		strBuff.append("distanceInterval = ").append(minimumInterval).append(".");

		return strBuff.toString();
	}
	public static String getTestStr(String scop,String mcpttId,int timeInterval,int distanceInterval)
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">");
		strBuff.append("<Configuration ConfigScope=\"").append(scop).append("\">");
		strBuff.append("<TriggeringCriteria>");
		strBuff.append("<GeographicalAreaChange>");

		strBuff.append("<PeriodicReport TriggerId=\"").append(mcpttId).append("\">").append(timeInterval).append("</PeriodicReport>");
		strBuff.append("<TravelledDistance TriggerId=\"").append(mcpttId).append("\">").append(distanceInterval).append("</TravelledDistance>");

		strBuff.append("</GeographicalAreaChange>");
		strBuff.append("</TriggeringCriteria>");
		strBuff.append("</Configuration>");
		strBuff.append("</location-info>");

		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strBuff.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
